package monitor

import (
	"fmt"
	"context"
	"github.com/prometheus/client_golang/api"
	"github.com/prometheus/client_golang/api/prometheus/v1"
	"time"
	"github.com/prometheus/common/model"
)

//func (h Host) PushGauge(key string) (alertData interface{})  {
//	alertData = h.fetchEstabAndTWVaule(key)
//	return
//
//}

//func (h Host) PushHistogram(key string) (alertData interface{})  {
//	alertData = h.fetchHistogramVaule(key)
//	return
//
//}

func (h Host)FetchEstabAndTWGauge(mertic string) (result float64)  {
	cfg := api.Config{
		Address: "http://192.168.138.128:9090",
	}
	proCli,err := api.NewClient(cfg)
	if err != nil {
		fmt.Println("connect to err is" , err.Error())
	}
	proApi := v1.NewAPI(proCli)
	ctx := context.Background()

	q := fmt.Sprintf("%s{instance=~'%s.*'}",mertic,string(h))
	res,errr := proApi.Query(ctx,q,time.Time{})
	if errr != nil {
		fmt.Println("query error is",errr.Error())
	}
	m,ok := res.(model.Vector)
	if ok && m.Len() > 0 {
		//d = append(d,float64(m[0].Value))
		fmt.Println("tcp connect status is " , m[0].Value)
		result = float64(m[0].Value)
	}
	return

}

//默认平均5分钟平均值
func (h Host)FetchDiskRWRateHistogram(mertic string) (result float64)  {
	cfg := api.Config{
		Address: "http://192.168.138.128:9090",
	}
	proCli,err := api.NewClient(cfg)
	if err != nil {
		fmt.Println("connect to err is" , err.Error())
	}
	proApi := v1.NewAPI(proCli)
	ctx := context.Background()


	//q := fmt.Sprintf("%s{instance=~'%s.*'}",mertic,string(h))
	q := fmt.Sprintf("irate(%s{instance=~'%s.*',device=~'sda'}[5m])",mertic,string(h))
	fmt.Println("debug here ,i douat that q is" ,q )
	res,errr := proApi.Query(ctx,q,time.Time{})
	if errr != nil {
		fmt.Println("query error is",errr.Error())
	}
	m,ok := res.(model.Vector)
	if ok && m.Len() > 0 {
		//d = append(d,float64(m[0].Value))
		fmt.Println("tcp connect status is " , m[0].Value)
		result = float64(m[0].Value) / 1024   //Kb
	}

	return

}


func (h Host)FetchDiskRWIOPSHistogram(mertic string) (result float64)  {
	cfg := api.Config{
		Address: "http://192.168.138.128:9090",
	}
	proCli,err := api.NewClient(cfg)
	if err != nil {
		fmt.Println("connect to err is" , err.Error())
	}
	proApi := v1.NewAPI(proCli)
	ctx := context.Background()
	//q := fmt.Sprintf("%s{instance=~'%s.*'}",mertic,string(h))
	q := fmt.Sprintf("irate(%s{instance=~'%s.*',device=~'sda'}[5m])",mertic,string(h))
	fmt.Println("debug here ,i douat that q is" ,q )
	res,errr := proApi.Query(ctx,q,time.Time{})
	if errr != nil {
		fmt.Println("query error is",errr.Error())
	}
	m,ok := res.(model.Vector)
	if ok && m.Len() > 0 {
		//d = append(d,float64(m[0].Value))
		fmt.Println("tcp connect status is " , m[0].Value)
		result = float64(m[0].Value)
	}

	return

}


func (h Host)FetchNetworkIOHistogram(mertic string) (result float64)  {
	cfg := api.Config{
		Address: "http://192.168.138.128:9090",
	}
	proCli,err := api.NewClient(cfg)
	if err != nil {
		fmt.Println("connect to err is" , err.Error())
	}
	proApi := v1.NewAPI(proCli)
	ctx := context.Background()
	//q := fmt.Sprintf("%s{instance=~'%s.*'}",mertic,string(h))
	q := fmt.Sprintf("irate(%s{instance=~'%s.*',device=~'ens33.*'}[5m])",mertic,string(h))
	fmt.Println("debug here ,i douat that q is" ,q )
	res,errr := proApi.Query(ctx,q,time.Time{})
	if errr != nil {
		fmt.Println("query error is",errr.Error())
	}
	m,ok := res.(model.Vector)
	if ok && m.Len() > 0 {
		//d = append(d,float64(m[0].Value))
		fmt.Println("tcp connect status is " , m[0].Value)
		result = float64(m[0].Value) / 1024    //kb
	}

	return

}
